﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;

namespace TCG
{
	/// <summary>
	/// 项目的控制面板.
	/// </summary>
	public class ProjectToolWindow : EditorWindow
	{
		[MenuItem ("Project/Settings")]
		static void Initialize ()
		{
			ProjectToolWindow window = EditorWindow.GetWindow<ProjectToolWindow> ();
			window.Show ();
		}

		void OnGUI ()
		{
			if (NGUIEditorTools.DrawHeader ("Serialization"))
			{
				NGUIEditorTools.BeginContents ();

				DrawBuildAllMaterialButton ();
				DrawBuildAllTextureButton ();
				DrawBuildAllPrefabsButton ();

				NGUIEditorTools.EndContents ();
			}
		}

		void DrawBuildAllMaterialButton ()
		{
			GUILayout.Label ("Build Materials");
			GUILayout.BeginHorizontal ();
			if (GUILayout.Button ("Build Materials ", GUILayout.Width (140)))
			{
				IList<Material> mats = GameEditorTools.CollectAll<Material> (GameEditorTools.SerializableDirName);
				foreach (Material _mat in mats)
				{
					try
					{
						MaterialSerializer.Serialize (_mat);
					}
					catch (System.Exception e)
					{
						Debug.LogError (e);
						Debug.Log ("Exception when building materials! Operation terminated!");
						break;
					}
				}
				Debug.Log ("All materials have been updated successfully!");
			}

			if (GUILayout.Button ("Build Material Map", GUILayout.Width (140)))
			{
				Dictionary<uint, string> idPathMap = new Dictionary<uint, string> ();
				IList<Material> mats = GameEditorTools.CollectAll<Material> (GameEditorTools.SerializableDirName);
				
				foreach (Material _mat in mats)
				{
					uint matId = CommonTools.ParseAssetIdFromName (_mat.name);
					string streamingPath = GameEditorTools.GetRelativeAssetPathNoSuffix (_mat);

					idPathMap.Add (matId, streamingPath);
				}

				AssetIdPathMap pathMap = new AssetIdPathMap ();
				pathMap.idPathMap = idPathMap;
				FileStream fs = new FileStream (CommonTools.GetStreamingPath ("Config/material_map"), FileMode.Create);
				ProtoBuf.Serializer.Serialize<AssetIdPathMap> (fs, pathMap);
				fs.Close ();
				AssetDatabase.Refresh ();

			}
			GUILayout.EndHorizontal ();
			GUILayout.Space (3.0f);
		}

		void DrawBuildAllTextureButton ()
		{
			GUILayout.Label ("打包贴图");
			GUILayout.BeginHorizontal ();
			if (GUILayout.Button ("Build Textures ", GUILayout.Width (140)))
			{
				IList<Texture> textures = GameEditorTools.CollectAll<Texture> (GameEditorTools.SerializableDirName);
				foreach (Texture _texture in textures)
				{
					try
					{
						TextureSerializer.Serialize (_texture);
					}
					catch (System.Exception e)
					{
						Debug.LogError (e);
						Debug.Log ("Exception when building materials! Operation terminated!");
						break;
					}
				}
			}
			
			if (GUILayout.Button ("Build Texture Map", GUILayout.Width (140)))
			{
				Dictionary<uint, string> idPathMap = new Dictionary<uint, string> ();
				IList<Texture> textures = GameEditorTools.CollectAll<Texture> (GameEditorTools.SerializableDirName);
				foreach (Texture _texture in textures)
				{
					uint textureId = CommonTools.ParseAssetIdFromName (_texture.name);
					string texturePath = GameEditorTools.GetRelativeAssetPathNoSuffix (_texture);
					idPathMap.Add (textureId, texturePath);
				}

				AssetIdPathMap pathMap = new AssetIdPathMap ();
				pathMap.idPathMap = idPathMap;
				FileStream fs = new FileStream (CommonTools.GetStreamingPath ("Config/texture_map"), FileMode.Create);
				ProtoBuf.Serializer.Serialize<AssetIdPathMap> (fs, pathMap);
				fs.Close ();
				AssetDatabase.Refresh ();
            }
            GUILayout.EndHorizontal ();
            GUILayout.Space (3.0f);
		}

		void DrawBuildAllPrefabsButton ()
		{
			GUILayout.Label ("打包预设");
			GUILayout.BeginHorizontal ();
			if (GUILayout.Button ("Build Prefabs ", GUILayout.Width (140)))
			{
				IList<GameObject> prefabs = GameEditorTools.CollectAll<GameObject> (GameEditorTools.SerializableDirName);
				foreach (GameObject _prefab in prefabs)
				{
					try
					{
						PrefabSerializer.Serialize (_prefab);
					}
					catch (System.Exception e)
					{
						Debug.LogError (e);
						Debug.Log ("Exception when building prefabs! Operation terminated!");
						break;
					}

				}
			}
			
			if (GUILayout.Button ("Build Prefab Map", GUILayout.Width (140)))
			{

				Dictionary<uint, string> idPathMap = new Dictionary<uint, string> ();
				IList<GameObject> prefabs = GameEditorTools.CollectAll<GameObject> (GameEditorTools.SerializableDirName);
				foreach (GameObject _prefab in prefabs)
				{
					uint prefabId = CommonTools.ParseAssetIdFromName (_prefab.name);
					string prefabPath = GameEditorTools.GetRelativeAssetPathNoSuffix (_prefab);
					idPathMap.Add (prefabId, prefabPath);
				}
				
				AssetIdPathMap pathMap = new AssetIdPathMap ();
				pathMap.idPathMap = idPathMap;
				FileStream fs = new FileStream (CommonTools.GetStreamingPath ("Config/prefab_map"), FileMode.Create);
				ProtoBuf.Serializer.Serialize<AssetIdPathMap> (fs, pathMap);
				fs.Close ();
				AssetDatabase.Refresh ();
            }
            GUILayout.EndHorizontal ();
            GUILayout.Space (3.0f);
        }
        
        
    }
}
